#include "mclinesegmentparametricwindow.h"
#include "ui_mclinesegmentparametricwindow.h"
#include <qvalidator.h>
#include <iostream>

MCLineSegmentParametricWindow::MCLineSegmentParametricWindow(MCCommand *command, MCSegmentVisual *segmentVisual, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MCLineSegmentParametricWindow),
    _segmentVisual(segmentVisual),
    _lineSegment(segmentVisual->lineSegment()),
    _command(command)
{
    ui->setupUi(this);


    ui->lineEdit->setText(QString::number(_lineSegment.startPoint().x));
    ui->lineEdit_2->setText(QString::number(_lineSegment.startPoint().y));
    ui->lineEdit_3->setText(QString::number(_lineSegment.endPoint().x));
    ui->lineEdit_4->setText(QString::number(_lineSegment.endPoint().y));
    ui->lineEdit_5->setText(QString::number(_lineSegment.length()));
    ui->lineEdit_6->setText(QString::number(_lineSegment.angle()));
}

MCLineSegmentParametricWindow::~MCLineSegmentParametricWindow()
{
    delete ui;
}

void MCLineSegmentParametricWindow::on_lineEdit_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double x1 = arg1.toDouble();
        _lineSegment.update(MCPoint2D(x1, _lineSegment.startPoint().y), _lineSegment.endPoint());
    }

    resetLength();
    updateValues();
}


void MCLineSegmentParametricWindow::on_lineEdit_2_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double y1 = arg1.toDouble();
        _lineSegment.update(MCPoint2D(_lineSegment.startPoint().x, y1), _lineSegment.endPoint());
    }

    resetLength();
    updateValues();
}


void MCLineSegmentParametricWindow::on_lineEdit_3_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double x2 = arg1.toDouble();
        _lineSegment.update(_lineSegment.startPoint(), MCPoint2D(x2, _lineSegment.endPoint().y));
    }

    resetLength();
    updateValues();
}


void MCLineSegmentParametricWindow::on_lineEdit_4_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double y2 = arg1.toDouble();
        _lineSegment.update(_lineSegment.startPoint(), MCPoint2D(_lineSegment.endPoint().x, y2));
    }

    resetLength();
    updateValues();
}


void MCLineSegmentParametricWindow::on_lineEdit_5_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double length = arg1.toDouble();
        _lineSegment = MCLineSegment2D(_lineSegment.startPoint(), _lineSegment.direction(), length);
    }

    resetPoints();
    updateValues();
}


void MCLineSegmentParametricWindow::on_lineEdit_6_textChanged(const QString &arg1)
{

}


void MCLineSegmentParametricWindow::on_pushButton_clicked()
{
    this->close();
    _command->finalize();
}


void MCLineSegmentParametricWindow::on_pushButton_2_clicked()
{
    this->close();
    _command->terminate();
}

void MCLineSegmentParametricWindow::updateValues()
{
    _segmentVisual->update(_lineSegment);
    _command->presenter_()->redraw();
}

void MCLineSegmentParametricWindow::resetLength()
{
    ui->lineEdit_5->setText(QString::number(_lineSegment.length()));
}

void MCLineSegmentParametricWindow::resetPoints()
{
    ui->lineEdit->setText(QString::number(_lineSegment.startPoint().x));
    ui->lineEdit_2->setText(QString::number(_lineSegment.startPoint().y));
    ui->lineEdit_3->setText(QString::number(_lineSegment.endPoint().x));
    ui->lineEdit_4->setText(QString::number(_lineSegment.endPoint().y));
}

bool MCLineSegmentParametricWindow::isTextValid(QString text)
{
    QDoubleValidator validator;
    int pos = 0;
    QValidator::State state = validator.validate(text, pos);

    if (state == QValidator::Acceptable)
    {
        return true;
    }
      
    return false;
}

